package 剑指offer;

import java.util.Stack;

public class p31栈的压入弹出序列 {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        if (pushed.length != popped.length) {
            return false;
        }

        Stack<Integer> stack=new Stack<>();
        int i=0;

        for (int num:pushed) {
            stack.add(num);
            while (!stack.isEmpty() && stack.peek()==popped[i]) {
                //stack.isEmpty()  参考 1234  3214
                stack.pop();
                i++;
            }
        }

        return stack.isEmpty();

    }
}
